﻿using fastJSON;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int count = 10000;
            var obj = User.TestUser();
            count = 100;
            TestJavaScriptSerializer(count, obj);
            TestNewtonsoftJson(count, obj);
            TestJayrockJson(count, obj);
            TestFastJson(count, obj);

            count = 100000;
            TestJavaScriptSerializer(count, obj);
            TestNewtonsoftJson(count, obj);
            TestJayrockJson(count, obj);
            TestFastJson(count, obj);
        }


        //测试FastJson性能
        static void TestFastJson(int count, object obj)
        {
            Stopwatch sw = new Stopwatch();
            JSONParameters p = new JSONParameters();
            p.EnableAnonymousTypes
                = p.ShowReadOnlyProperties
                = p.UseEscapedUnicode
                = p.UseExtensions
                = p.UseFastGuid
                = p.UseOptimizedDatasetSchema
                = p.UseUTCDateTime
                = p.UsingGlobalTypes
                = false;
            Console.WriteLine();
            Console.WriteLine("fastJSON.NET 每次" + count + " 共10次");
            for (int j = 0; j < 10; j++)
            {
                sw.Restart();
                for (int i = 0; i < count; i++)
                {
                    fastJSON.JSON.ToJSON(obj, p);
                }
                sw.Stop();
                Console.Write(sw.ElapsedMilliseconds + "ms | ");
            }

            Console.WriteLine();
            Console.WriteLine("结果:" + fastJSON.JSON.ToJSON(obj, p));
            Console.WriteLine("==============================================");
        }
        //测试JayrockJson性能
        static void TestJayrockJson(int count, object obj)
        {
            Stopwatch sw = new Stopwatch();

            Console.WriteLine();
            Console.WriteLine("Jayrock.Json 每次" + count + " 共10次");
            for (int j = 0; j < 10; j++)
            {
                sw.Restart();
                for (int i = 0; i < count; i++)
                {
                    var writer = new Jayrock.Json.JsonTextWriter();
                    Jayrock.Json.Conversion.JsonConvert.Export(obj, writer);
                    string str = writer.ToString();
                }
                sw.Stop();
                Console.Write(sw.ElapsedMilliseconds + "ms | ");
            }
            var w = new Jayrock.Json.JsonTextWriter();
            Jayrock.Json.Conversion.JsonConvert.Export(obj, w);

            Console.WriteLine();
            Console.WriteLine("结果:" + w.ToString());
            Console.WriteLine("==============================================");
        }

        static void TestNewtonsoftJson(int count, object obj)
        {
            Stopwatch sw = new Stopwatch();

            Console.WriteLine();
            Console.WriteLine("Newtonsoft.Json 每次" + count + " 共10次");
            for (int j = 0; j < 10; j++)
            {
                sw.Restart();
                for (int i = 0; i < count; i++)
                {
                    Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                }
                sw.Stop();
                Console.Write(sw.ElapsedMilliseconds + "ms | ");
            }
            var w = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

            Console.WriteLine();
            Console.WriteLine("结果:" + w.ToString());
            Console.WriteLine("==============================================");
        }

        static void TestJavaScriptSerializer(int count, object obj)
        {


            Stopwatch sw = new Stopwatch();

            Console.WriteLine();
            Console.WriteLine("JavaScriptSerializer 每次" + count + " 共10次");
            for (int j = 0; j < 10; j++)
            {
                sw.Restart();
                for (int i = 0; i < count; i++)
                {
                    new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(obj);
                }
                sw.Stop();
                Console.Write(sw.ElapsedMilliseconds + "ms | ");
            }
            var w = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(obj);

            Console.WriteLine();
            Console.WriteLine("结果:" + w.ToString());
            Console.WriteLine("==============================================");
        }



    }
}
